/**
 * @param {number[]} temperatures
 * @return {number[]}
 */
var dailyTemperatures = function(temperatures) {
  // 数组解决
  // let result = new Array(temperatures.length).fill(0);
  // for(let i = 0; i < temperatures.length; i++) {
  //   for(let j = i + 1; j < temperatures.length; j++) {
  //     if(temperatures[i] < temperatures[j]) {
  //       result[i] = j - i;
  //       break;
  //     }
  //   }
  // }
  // 栈解决
  let stack = [0], result = new Array(temperatures.length).fill(0);
  for(let i = 1; i < temperatures.length; i++) {
    while(stack.length && temperatures[i] > temperatures[stack[stack.length - 1]]) {
      result[stack[stack.length - 1]] = (i - stack[stack.length - 1]);
      stack.pop();
    }
    stack.push(i);
  }
  return result;
};
let temperatures = [73,74,75,71,69,72,76,73];
console.log(dailyTemperatures(temperatures));
temperatures = [30,40,50,60];
console.log(dailyTemperatures(temperatures));
temperatures = [30,60,90];
console.log(dailyTemperatures(temperatures));
